#include "appglobaldata.h"
#include "BLMC.h"
#include "structdefine.h"


void Manual()
{
    switch(Phase)
    {
    case 0:	//AB
        PWM_A_ON;
        MUTEX_NEG_B_ON;
        SENSE_C;
        SENSE_RISING_INT;
        break;
    case 1:	//AC
        PWM_A_ON;
        MUTEX_NEG_C_ON;
        SENSE_B;
        SENSE_FALLING_INT;
        break;
    case 2:	//BC
        PWM_B_ON;
        MUTEX_NEG_C_ON;
        SENSE_A;
        SENSE_RISING_INT;
        break;
    case 3:	//BA
        PWM_B_ON;
        MUTEX_NEG_A_ON;
        SENSE_C;
        SENSE_FALLING_INT;
        break;
    case 4:	//CA
        PWM_C_ON;
        MUTEX_NEG_A_ON;
        SENSE_B;
        SENSE_RISING_INT;
        break;
    case 5:	//CB
        PWM_C_ON;
        MUTEX_NEG_B_ON;
        SENSE_A;
        SENSE_FALLING_INT;
        break;								 
    }
}

void commutate(void)
{
    CompBlankCNT = 0; // blank filtering during commtutation time/*1:7 Postscaler,delay 8 PWM period to check ZC*/
 
    zc_detected = 0; // clear our state variables
    bemf_filter = 0;
    comm_time = 0;
    comm_done = 1;
	SystemState = ZeroCrossDetect;	
    switch(Phase)
    {
    case 0:	//AB
        PWM_A_ON;
            MUTEX_NEG_C_ON;
            SENSE_RISING_INT;
            SENSE_B;
            Phase++;

            LEDG = ~LEDG;
        break;

    case 1:	//AC
        MUTEX_NEG_C_ON;
            PWM_B_ON;
            SENSE_A;
            SENSE_FALLING_INT;
            Phase++;

            LEDG = ~LEDG;
        break;

    case 2:	//BC
        PWM_B_ON;
            MUTEX_NEG_A_ON;
            SENSE_C;
            SENSE_RISING_INT;
            Phase++;
            LEDG = ~LEDG;
        break;

    case 3:	//BA
        MUTEX_NEG_A_ON;
            PWM_C_ON;
            SENSE_B;
            SENSE_FALLING_INT;
            Phase++;
            LEDG = ~LEDG;
        break;

    case 4:	//CA
        PWM_C_ON;
            MUTEX_NEG_B_ON;
            SENSE_A;
            SENSE_RISING_INT;
            Phase++;
            LEDG = ~LEDG;
        break;

    case 5:	//CB
        MUTEX_NEG_B_ON;
            PWM_A_ON;
            SENSE_C;
            SENSE_FALLING_INT;
            Phase = 0;
            LEDG = ~LEDG;
        break;
    }
} // end commutate